<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ include file="/WEB-INF/jsp/common/include.jsp" %>

<html>
    <head>
        <title>Novadmin - Authentification</title>
        
        <c:url value="/secure/action/login.html" var="loginUrl" />
        <c:url value="/home.html" var="homeUrl" />
        <c:url value="/secure/action/password/reset.html" var="resetUrl" />
                
        <script type="text/javascript"> 
            $().ready(function(){

                // validate the form when it is submitted
                var container = $("#user-login-form-error");
                var validator = $("#user-login-form").bind("invalid-form.validate", function() {

                }).validate({
                    errorLabelContainer: $("#autoError", container),
                    errorContainer: container,
                    errorElement: "li",
                    rules: {
                        j_username: {
                            required: true
                        },
                        j_password: {
                            required: true
                        }
                    },
                    messages: { 
                        j_username: "Le champ Nom d'utilisateur est obligatoire.",
                        j_password: "Le champ Mot de passe est obligatoire."
                    },
                    submitHandler: function(data){
                        loader.show();
                        
                        // reset
                        $("#manuelError").text("");
                        
                        $.ajax({
                            type    : "POST",
                            cache   : false,
                            url     : "${loginUrl}",
                            data    : $("#user-login-form").serializeArray(),
                            success: function(data){
                                loader.hide();
                                if(data == true){
                                    document.location.href = "${homeUrl}";
                                }else{
                                    $("<li>Les informations d'authentification ne sont pas correctes</li>").appendTo("#manuelError");
                                    $("#user-login-form-error").show();
                                    $("#user-login-form-error ul").show();
                                }
                            }
                        });
        
                        return false;
                    } 
                });
            });
            
            
            function resetPassword(){
                var login = $("#j_username").val();
                
                // reset
                $("#manuelError").text("");
                
                if(login == null || login == ""){
                    $("<li>Le champ Nom d'utilisateur est obligatoire.</li>").appendTo("#manuelError");
                    $("#user-login-form-error").show();
                    $("#user-login-form-error ul").show();
                    
                    return;
                }
                
                loader.show();
                $.ajax({
                    type    : "POST",
                    cache   : false,
                    url     : "<c:url value='/secure/action/password/reset.html' />",
                    data    : 'login=' + login,
                    success: function(data){
                        loader.hide();
                        if(data == true){
                           $("<li>La demande a été prise en compte.</li>").appendTo("#manuelOk");
                           $("#user-login-form-ok").fadeIn(300).delay(2000).fadeOut('slow');
                        }
                    }
                });
            }
        </script>
    </head>
    <body>
       <table border="0" cellpadding="0" cellspacing="0" id="content">
            <tbody>
                <tr>
                    <td valign="top">
                         <page:applyDecorator name="content">
                             <page:param name="title">Connexion à l'application</page:param>
                            
                             <!-- BLOC OK -->
                             <div id="user-login-form-ok" class="messages-ok" style="display: none">
                                <ul id="manuelOk">
                                </ul>
                             </div>
                             
                             <!-- BLOC ERREUR -->
                             <div id="user-login-form-error" class="messages error" style="display: none">
                                <ul id="autoError">
                                </ul>
                                <ul id="manuelError">
                                </ul>
                             </div>
                            
                             <!-- BLOC AUTHENTIFICATION -->
                             <div class="blockUser block-user" id="block-user-0">
                                  <h2 class="titleUser">Novadmin: Infos d'authentification</h2>
                                  <div class="content">
                                      <form action="" accept-charset="UTF-8" method="post" id="user-login-form" name="user-login-form">
                                          <div class="form-item" id="edit-name-wrapper">
                                              <label for="edit-name">Login NTS&nbsp;: <span class="form-required" title="Ce champ est obligatoire.">*</span></label>
                                              <input type="text" maxlength="255" name="j_username" id="j_username" size="25" value="" autocomplete="off">
                                          </div>
                                          <div class="form-item" id="edit-pass-wrapper">
                                              <label for="edit-pass">Mot de passe&nbsp;: <span class="form-required" title="Ce champ est obligatoire.">*</span></label>
                                              <input type="password" name="j_password" id="j_password" maxlength="255" size="25" autocomplete="off">
                                          </div>
                                          <input type="submit" name="op" id="edit-submit" value="Se connecter" class="form-submit">
                                          <div class="item-list">
                                              <ul>
                                                  <li class="first last">
                                                      <a href="javascript:resetPassword();" title="Demander un nouveau mot de passe par courriel.">Demander un nouveau mot de passe par email</a>
                                                  </li>
                                              </ul>
                                          </div>
                                      </form>
                                  </div>
                              </div>
                        </page:applyDecorator>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>